//
//  BBFullScreenController.m
//  xNose
//
//  Created by ben smith on 9/8/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBFullScreenController.h"
#import "BBNodeController.h"
#import "BBFullScreenWindow.h"


@implementation BBFullScreenController

-(IBAction)toggleFullScreen:(id)sender
{
	[[BBNodeController sharedNodeController] zoomNotification:[NSNumber numberWithFloat:1.0]];
	if (fullScreenWindow == nil) {
		[self goFullScreen:nil];
		return;
	}	
	[self closeFullScreenWindow:self];
}

-(IBAction)goFullScreen:(id)sender;
{
	int windowLevel;
	
	NSScreen * chosenScreen = [NSScreen mainScreen];
	//if (CGCaptureAllDisplaysWithOptions(kCGCaptureNoFill) != kCGErrorSuccess) {
		//	if (CGCaptureAllDisplays() != kCGErrorSuccess) {
	//	NSLog( @"Couldn't capture the display!" );
	//}
	// Get the shielding window level
	windowLevel = CGShieldingWindowLevel();
	// Get the screen rect of our main display
	NSRect screenRect = [chosenScreen frame];//[chosenScreen frame];
	// Put up a new window
	NSPoint p = [[BBNodeController sharedNodeController] nodeSnapPoint];
	saveSnap = p;
	p.y = NSHeight(screenRect);
	[[BBNodeController sharedNodeController] setNodeSnapPoint:p];
	
	fullScreenWindow = [[BBFullScreenWindow alloc] initWithContentRect:screenRect
																													 styleMask:NSBorderlessWindowMask
																														 backing:NSBackingStoreBuffered
																															 defer:NO
																															screen:chosenScreen];
	[fullScreenWindow setLevel:windowLevel];
	[fullScreenWindow setFrame:screenRect display:YES]; // do this twice for some reason, otherwise you sometimes get the wrong screen rect
	[fullScreenWindow setBackgroundColor:[NSColor blackColor]];
	[fullScreenWindow makeKeyAndOrderFront:nil];
	
	[[BBNodeController sharedNodeController] loadViewInto:[fullScreenWindow contentView]];
	
	[fullScreenWindow makeFirstResponder:[fullScreenWindow contentView]];
}

-(IBAction)closeFullScreenWindow:(id)sender
{
	[[BBNodeController sharedNodeController] setNodeSnapPoint:saveSnap];
	if (fullScreenWindow == nil) return;

	//[[BBNodeController sharedNodeController] refreshView:nil];
		
	[fullScreenWindow orderOut:self];
	[fullScreenWindow release];
	fullScreenWindow = nil;
	// Release the display(s)
	//NSNumber * defaultNum =	[[NSUserDefaults standardUserDefaults] objectForKey:@"BBDefaultScreenNumber"];
	//CGDirectDisplayID displayID = [defaultNum intValue];
	//if (CGDisplayRelease( displayID ) != kCGErrorSuccess) {
	
	if (CGReleaseAllDisplays() != kCGErrorSuccess) {
		// need to make this a bit more friendly
		NSLog( @"Couldn't release the display(s)!" );
	}
	
	[[BBNodeController sharedNodeController] resetDefaultView];
	[[BBNodeController sharedNodeController] refreshView:self];
	
}

@end
